/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.view;

import br.jm.financeiro.datamodel.LazyPropostaEmail;
import br.jm.financeiro.entity.PropostaEmail;
import br.jm.financeiro.sistema.eao.PropostaEmailEao;
import java.io.Serializable;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author joao
 */
public class PropostaEmailBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private PropostaEmail selectedPropostaEmail;
    private LazyDataModel<PropostaEmail> lazyModel;
    private final PropostaEmailEao propostaEmailEAO;
    private FacesMessage msg = null;
    private boolean altera;
    private String itemlogo;
    private String itemmacro;
    private String cEditor;

    public PropostaEmailBean() {
        propostaEmailEAO = PropostaEmailEao.getInstance();
        selectedPropostaEmail = new PropostaEmail();

        lazyModel = new LazyPropostaEmail(propostaEmailEAO);
    }

    public PropostaEmail getSelectedPropostaEmail() {
        return selectedPropostaEmail;
    }

    public void setSelectedPropostaEmail(PropostaEmail selectedPropostaEmail) {
        this.selectedPropostaEmail = selectedPropostaEmail;
    }

    public LazyDataModel<PropostaEmail> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<PropostaEmail> lazyModel) {
        this.lazyModel = lazyModel;
    }

    public void inserir(ActionEvent actionEvent) {
        try {
            selectedPropostaEmail.setIdEmail(null);

            propostaEmailEAO.create(selectedPropostaEmail);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Modelo de E-Mail Inserido com Sucesso: ".concat(selectedPropostaEmail.getDscEmail())), selectedPropostaEmail.getDscEmail());

        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Inserir" + selectedPropostaEmail.getDscEmail(), e.getMessage());
        } finally {
            selectedPropostaEmail = null;
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void alterar(ActionEvent actionEvent) {
        try {
            propostaEmailEAO.edit(selectedPropostaEmail);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Modelo de E-Mail Alterado com Sucesso: ".concat(selectedPropostaEmail.getDscEmail())), selectedPropostaEmail.getDscEmail());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Alterar: " + selectedPropostaEmail.getDscEmail(), e.getMessage());
        } finally {
            selectedPropostaEmail = null;
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void excluir(ActionEvent actionEvent) {
        try {
            propostaEmailEAO.destroy(selectedPropostaEmail);

            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, LoginBean.AtualizaLog("", "Modelo de E-Mail Excluido com Sucesso: ".concat(selectedPropostaEmail.getDscEmail())), selectedPropostaEmail.getDscEmail());
        } catch (Exception e) {
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro ao Excluir: " + selectedPropostaEmail.getDscEmail(), e.getMessage());
        } finally {
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void preparaInserir(ActionEvent actionEvent) {
        this.altera = true;
        this.selectedPropostaEmail = new PropostaEmail();
    }

    public boolean isAltera() {
        return altera;
    }

    public void setAltera(boolean altera) {
        this.altera = altera;
    }

    public String getItemlogo() {
        return itemlogo;
    }

    public void setItemlogo(String itemlogo) {
        this.itemlogo = itemlogo;
    }

    public String getItemmacro() {
        return itemmacro;
    }

    public void setItemmacro(String itemmacro) {
        this.itemmacro = itemmacro;
    }

    public void inserirLogo() {
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
        final String url = request.getRequestURI();
        String teste = request.getContextPath();
        String teste2 = request.getPathInfo();
        String teste3 = request.getPathTranslated();
        String teste4 = request.getProtocol();
        String teste5 = request.getServletPath();

        String tamanho;
        String altura;

        if (this.itemlogo.contains("patricia_rodape")) {
            tamanho = "1088";
            altura = "59";
        } else {
            tamanho = "274";
            altura = "127";
        }

        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map<String, String> map = facesContext.getExternalContext().getRequestParameterMap();

        this.cEditor = map.get("frmPropostaEmail:editorModelo_input");

        this.cEditor = this.cEditor.concat("<div align=\"center\"><img src=\"").concat(this.itemlogo).concat("\" width=\"").concat(tamanho).concat("\" height=\"").concat(altura).concat("\"/></div>");
        this.selectedPropostaEmail.setEmail(this.cEditor);
        this.itemlogo = "";
    }

    public void inserirMacro() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map<String, String> map = facesContext.getExternalContext().getRequestParameterMap();

        this.cEditor = map.get("frmPropostaEmail:editorModelo_input");

        this.cEditor = this.cEditor.concat(this.itemmacro);
        this.selectedPropostaEmail.setEmail(this.cEditor);
        this.itemmacro = "";
    }
}
